<?php

namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Session,Validator,Input;
use App\Models\User;
use App\Models\MessageCode;
use App\Interfaces\SmsService;
use App\Interfaces\UserService;
use App\Http\Requests\My\ResetPwdRequest;
use App\Http\Requests\Front\TellRequest;
use App\Http\Requests\My\FindPwdRequest;

class MyAccountController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //dd ($request->all());
        $step = "step_1";
        $user = Auth::user();
        $code = $request->get('code');
        $user_id = $request->get('uid');
        $email = $request->get('email');

        if($code&&$user_id){
            $ckcode = md5(User::where('id',$user->id)->where('code',$code)->pluck('code'));
            if($code=$ckcode&&$user_id=$user->id){
                $user->check_type=2;
                $user->email=$email;
                $user->save();
                return view('front.my.safe',compact('user','step'));
            }else{
                $user->check_type=3;
                $user->save();
                return view('errors.404');
            }
        }else{
            return view('front.my.safe',compact('user','step'));
        }


    }

    public function forgetPwd(Request $request,SmsService $sms){
        $phone = Input::get('phone');
        $mescode = Input::get('MSGCode');
        $ckCode = Input::get('Code');

        $message_id = Session::get('MsgCode_id');

        $validateRegCode = $sms->validateRegCode($message_id,$mescode);

            if($ckCode==Session::get('milkcaptcha')){
                if($validateRegCode){

                    $messageCode = MessageCode::find(Session::get('MsgCode_id'));

                    $messageCode->delete();

                    Session::put('phone',$phone);
                    Session::save();

                    return redirect()->back()->with('step','step_2');


                }else{
                    return redirect::back()->withInput()->with('message','短信验证码错误');
                }

            }else{
                return redirect::back()->withInput()->with('message','验证码错误');
            }




    }

    public function resetPaw(ResetPwdRequest $request,UserService $userService){
        //$user_name = Session::get('user_name');
        $user_name = Auth::user()->user_name;
        $oldPwd = Input::get('oldPwd');
        $newPwd = Input::get('newPwd');
        $rePwd = Input::get('password_confirmation');
        $code = Input::get('code');

        $oldPassword = User::where('user_name', $user_name)->pluck('Password');

        $rules = $request->rules();
        $messages = $request->messages();
        //dd($messages);
        $validator = Validator::make(Input::all(),$rules,$messages);

        if($validator->passes()){
            if($code==Session::get('milkcaptcha')){
                if(md5($oldPwd) == $oldPassword){
                    if(md5($oldPwd)!=md5($newPwd)){
                        if($newPwd==$rePwd){
                            $user = $userService->updatePaw($user_name,$oldPwd,$newPwd);
                            if($user){
                                return redirect()->back()->with('step','step_2');
//                                return redirect::to('/user/login')->with('step','step_2');
                            }else{
                                return redirect()->back()->withInput()->with('message','密码修改失败！');
                            }
                        }else{
                            return redirect()->back()->withInput()->with('message','密码确认不匹配');
                        }
                    }else{
                        return redirect()->back()->withInput()->with('message','本次修改密码和原密码一致');

                    }
                }else{
                    return redirect()->back()->withInput()->with('message','原密码错误');
                }

            }else{
                return redirect()->back()->withInput()->with('message','验证码错误');
            }


        }else{
            return redirect()->back()->with('message','请正确填写数据');
        }


    }

    public function sms(SmsService $sms, MessageCode $messageCode,Request $request)
    {
        $sendLiveTime=60;
        $phone = $request->phone;
        $is_exit = User::where('phone', $phone)->first();
        $sendTime = MessageCode::where('phone',$phone)->orderBy('send_time','desc')->pluck('send_time');
        if($is_exit ){
            if($sendTime){
                if(strtotime(date('Y-m-d H:i:s',time()))-strtotime($sendTime)>=$sendLiveTime){
                    $message_id =  $sms->sendRegMsg($phone);
                }else{
                    return response()->json(['status'=>'0','message'=>'该手机已发送验证码！']);
                }
            }else{
                $message_id =  $sms->sendRegMsg($phone);
            }
        }else{
            return response()->json(['status'=>'0','message'=>'该手机号未绑定！']);
        }

        if($message_id){
            Session::put('MsgCode_id',$message_id);
            Session::save();
            return response()->json(['status'=>'1','message'=>'发送成功！']);
        }else{
            return response()->json(['status'=>'0','message'=>'发送失败！']);
        }
    }


}
